package com.mywiki.controller;
import com.mywiki.domain.Ebook;
import com.mywiki.req.EbookQueryReq;
import com.mywiki.req.EbookSaveReq;
import com.mywiki.resp.CommonResp;
import com.mywiki.resp.EbookQueryResp;
import com.mywiki.resp.PageResp;
import com.mywiki.service.EbookService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * describe:
 *
 * @author: robin
 * @date 2024/1/19 11:19
 */
@RestController
@RequestMapping("/ebook")
public class EbookController {

    private static final Logger LOG = LoggerFactory.getLogger(EbookController.class);

    @Autowired
    private EbookService ebookService;

    @GetMapping("/list")
    public CommonResp list(@Valid EbookQueryReq req){
        LOG.info("req:{}", req);
        CommonResp<PageResp<EbookQueryResp>> resp = new CommonResp<>();
        PageResp<EbookQueryResp> list = ebookService.list(req);
        resp.setContent(list);
        return resp;
    }

    @PostMapping("/save")
    public CommonResp save(@Valid @RequestBody EbookSaveReq req){
        CommonResp resp = new CommonResp();
        ebookService.save(req);
        return resp;
    }

    @DeleteMapping("/delete/{id}")
    public CommonResp delete(@PathVariable String id){
        CommonResp resp = new CommonResp();
        ebookService.delete(Long.valueOf(id));
        LOG.info("删除完成:{}", id);
        return resp;
    }


}
